from django.test import TestCase
from django.urls import reverse
from rest_framework.test import APITestCase
from rest_framework import status
from .models import StockPerformance, SectorPerformance, MarketSummary
import datetime


class ModelTestCase(TestCase):
    """测试数据分析模型"""

    def setUp(self):
        # 创建测试数据
        self.today = datetime.date.today()
        
        # 创建股票表现数据
        StockPerformance.objects.create(
            stock_code='000001',
            stock_name='平安银行',
            date=self.today,
            period='day',
            open_price=10.0,
            close_price=10.5,
            high_price=11.0,
            low_price=9.8,
            volume=1000000,
            amount=10500000,
            change_percent=5.0,
            turnover_rate=2.5,
            sector='银行'
        )
        
        # 创建板块表现数据
        SectorPerformance.objects.create(
            sector='银行',
            date=self.today,
            period='day',
            stock_count=10,
            up_count=6,
            down_count=4,
            avg_change=2.5,
            max_change=8.0,
            min_change=-3.0,
            turnover_rate=3.2
        )
        
        # 创建市场概览数据
        MarketSummary.objects.create(
            date=self.today,
            period='day',
            total_stocks=3000,
            up_stocks=1500,
            down_stocks=1200,
            flat_stocks=300,
            avg_change=0.5,
            avg_turnover=3.0,
            total_amount=5000000000
        )

    def test_stock_performance_model(self):
        """测试股票表现模型"""
        stock = StockPerformance.objects.get(stock_code='000001')
        self.assertEqual(stock.stock_name, '平安银行')
        self.assertEqual(stock.sector, '银行')
        self.assertEqual(stock.change_percent, 5.0)

    def test_sector_performance_model(self):
        """测试板块表现模型"""
        sector = SectorPerformance.objects.get(sector='银行')
        self.assertEqual(sector.stock_count, 10)
        self.assertEqual(sector.up_count, 6)
        self.assertEqual(sector.avg_change, 2.5)

    def test_market_summary_model(self):
        """测试市场概览模型"""
        market = MarketSummary.objects.get(date=self.today)
        self.assertEqual(market.total_stocks, 3000)
        self.assertEqual(market.up_stocks, 1500)
        self.assertEqual(market.avg_change, 0.5)


class APITestCase(APITestCase):
    """测试数据分析API"""

    def setUp(self):
        # 创建测试数据
        self.today = datetime.date.today()
        
        # 创建股票表现数据
        StockPerformance.objects.create(
            stock_code='000001',
            stock_name='平安银行',
            date=self.today,
            period='day',
            open_price=10.0,
            close_price=10.5,
            high_price=11.0,
            low_price=9.8,
            volume=1000000,
            amount=10500000,
            change_percent=5.0,
            turnover_rate=2.5,
            sector='银行'
        )
        
        # 创建板块表现数据
        SectorPerformance.objects.create(
            sector='银行',
            date=self.today,
            period='day',
            stock_count=10,
            up_count=6,
            down_count=4,
            avg_change=2.5,
            max_change=8.0,
            min_change=-3.0,
            turnover_rate=3.2
        )
        
        # 创建市场概览数据
        MarketSummary.objects.create(
            date=self.today,
            period='day',
            total_stocks=3000,
            up_stocks=1500,
            down_stocks=1200,
            flat_stocks=300,
            avg_change=0.5,
            avg_turnover=3.0,
            total_amount=5000000000
        )

    def test_stock_performance_list(self):
        """测试股票表现列表API"""
        url = reverse('stockperformance-list')
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data['results']), 1)

    def test_sector_performance_list(self):
        """测试板块表现列表API"""
        url = reverse('sectorperformance-list')
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data['results']), 1)

    def test_market_summary_list(self):
        """测试市场概览列表API"""
        url = reverse('marketsummary-list')
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data['results']), 1)

    def test_top_stocks_api(self):
        """测试获取表现最好的股票API"""
        url = reverse('stockperformance-top-stocks')
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_sector_strength_api(self):
        """测试获取板块强度API"""
        url = reverse('sectorperformance-sector-strength')
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_market_overview_api(self):
        """测试获取市场概览API"""
        url = reverse('marketsummary-market-overview')
        response = self.client.get(url)
        self.assertEqual(response.status_code, status.HTTP_200_OK)